package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Gift;
import cn.wolfcode.crm.domain.StockOutComeBill;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.GiftQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IGiftService;
import cn.wolfcode.crm.service.IStockOutcomeBillService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

@Controller
@RequestMapping("gift")
public class GiftController {
    @Autowired
    private IGiftService giftService;
    @Autowired
    private IStockOutcomeBillService stockOutcomeBillService;

    //用来显示页面的
    @RequestMapping("list")
    public String list(){
        return "gift/list";
    }

    //后台查询显示的数据
    @ResponseBody
    @RequestMapping("query")
    public PageResult query(GiftQueryObject qo){
        PageResult pageResult = giftService.query(qo);
        return pageResult;
    }





    //修改和保存操作
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    public JSONResult saveOrUpdate(StockOutComeBill stock){
        JSONResult jsonResult = new JSONResult();
        try {
        //新增礼品出库单
            stock.setOutType(StockOutComeBill.GIFT);
            String s = UUID.randomUUID().toString().substring(0, 8);

            stock.setSn(s);
            stockOutcomeBillService.saveOrUpdate(stock);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark1("操作失败" );
        }
        return jsonResult;
    }
    @ResponseBody
    @RequestMapping("exchangeGift")
    public JSONResult saveOrUpdate(Gift gift,String cardNumber){
        JSONResult jsonResult = new JSONResult();
        try {
            giftService.exchangeGift(gift,cardNumber);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark1(e.getMessage());
        }
        return jsonResult;
    }

}
